package com.mes.web.controller.cb;

import com.mes.entity.cb.Cnum;
import com.mes.entity.cb.MaStock;
import com.mes.entity.cb.Materiel;
import com.mes.entity.cb.Pages;
import com.mes.service.cb.MaStockService;
import com.mes.service.cb.MaterieService;
import com.mes.utils.CustomUtils;
import com.mes.utils.ResponseUtil;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MaStockController
 * @Description: 资材库存
 * @Author cb
 * @Date 2018/4/18 14:45
 * @version 1.0
 */
@Controller
@RequestMapping("/maStock")
public class MaStockController {

    @Resource
    private MaStockService maStockService;

    @Resource
    private MaterieService materieService;






    @RequestMapping("/maStock_list")
    public void maStock_list(HttpServletResponse response, String limit, String offset) throws Exception {
        Pages pages = CustomUtils.pagings(limit, offset);
        List<Cnum> list =  maStockService.maStock_list(pages);

        int total = maStockService.maStock_listTotal(pages);

        JSONObject jsonObject = new JSONObject();
        /*JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);*/
        jsonObject.put("total", total);
        jsonObject.put("rows", list);
        ResponseUtil.write(response, jsonObject);
    }

    @RequestMapping("/maStock_import")
    public void maStock_import(HttpServletRequest request , HttpServletResponse response , @RequestParam(value = "pic", required = false)MultipartFile file) throws Exception {
        //文件是否为空
        if (file == null) {
            return;
        }

        JSONObject jsonObject =new JSONObject();

        //文件路径
        String  path = request.getSession().getServletContext().getRealPath("upload");

        //文件名称
        String filename = file.getOriginalFilename();

        InputStream is = file.getInputStream();

        File dir = new File(path,filename);

        if (!dir.exists()) {
            dir.mkdirs();
        }

        file.transferTo(dir);


        List<Materiel> list = new ArrayList<>();

        Workbook workbook = null;
        // 创建一个表格
        try {
            workbook = new HSSFWorkbook(is);
            // 获得sheet1
        } catch (Exception e) {
            workbook = new XSSFWorkbook(is);
        }

        // 获得sheet1
        Sheet sheet = workbook.getSheetAt(0);
        // 读取每一行数据

        List<MaStock> list1 = new ArrayList<>();

        for (Row row : sheet) {

            int rowNum = row.getRowNum();
            if (rowNum == 0) {
                continue;
            }


            MaStock maStock =new MaStock();

            Cell cell = row.getCell(0);

          //条码
            if (cell != null) {
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                maStock.setScode(cell.getStringCellValue());
            }

            //批号
            Cell cell1 = row.getCell(1);
            if (cell1 != null) {
                cell1.setCellType(HSSFCell.CELL_TYPE_STRING);

                maStock.setPihao(cell1.getStringCellValue());
            }

            //车种
            Cell cell2 = row.getCell(2);


            //物料名称
            Cell cell3 = row.getCell(3);


            //颜色
            Cell cell4 = row.getCell(4);


            //物料型号
            Cell cell5 = row.getCell(5);
            if (cell5 != null) {
                cell5.setCellType(HSSFCell.CELL_TYPE_STRING);

                Long maId = materieService.materie_selectByMaterialmodelMX(cell5.getStringCellValue());
                if (maId == 0 || maId == null) {
                    return;
                }
                maStock.setSmaid(maId);
            }

            //入库数量
            Cell cell6 = row.getCell(6);
            if (cell6 != null) {
                cell6.setCellType(HSSFCell.CELL_TYPE_STRING);

                try {

                    long l = Long.parseLong(cell6.getStringCellValue());
                    maStock.setSnum(l);
                }catch (Exception e) {
                    return ;
                }

            }

            //入库时间

            //操作人

            maStock.setSman("陈工");


            list1.add(maStock);


        }

        int total = maStockService.maStock_insert(list1);

        if (total>0) {

            jsonObject.put("success", true);
        } else {
            jsonObject.put("success", false);
        }

        ResponseUtil.write(response,jsonObject);

    }
    
    @RequestMapping("/sumByRuNumber")
    @ResponseBody
    public long sumByRuNumber(long rwid) {
    	long rsum = maStockService.sumByRuNumber(rwid);
    	
    	return rsum;
    }


}
